<?php
namespace  App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


/**
 * Class UploadController
 * @package App\Http\Controllers
 * 文件上传控制器
 */
class UploadController extends Controller
{
    public function index(){

    }

    public function save(){

    }

    public function update(){

    }

    public function delete(){

    }

    /*
     * 多图片上传
     */
    public function uploadImages(Request $request)
    {

        $file1 = $request->file('file1');//身份证正面
        $file2 = $request->file('file2');//身份证背面
        if (empty($file1) || empty($file2)) {
            return $this->return_json(201,'','上传失败');
        }
        if($file1->getClientSize() > 2097152 || $file2->getClientSize() > 2097152){
            return $this->return_json(202,'','单个文件最大允许上传2M!');
        }
        $image = array(
            'jpeg','png','jpg','gif'
        );
        $fileextension1 = $file1->getClientOriginalExtension();//文件名后缀
        $fileextension2 = $file2->getClientOriginalExtension();//文件名后缀
        if(!in_array($fileextension1,$image) || !in_array($fileextension2,$image)){
            return $this->return_json(203,'','允许上传的格式为jpeg,jpg,png,gif');
        }
        $imageName1 = $file1->getClientOriginalName();
        $imageName2 = $file2->getClientOriginalName();
        $newName1 = md5(date('ymdhis').$imageName1.rand(1000,9999)).".".$fileextension1;
        $newName2 = md5(date('ymdhis').$imageName2.rand(1000,9999)).".".$fileextension2;
        $time = date('Ymd', $_SERVER['REQUEST_TIME']);
        $uploadFolder = sprintf('%s/public/uploads/%s/','', $time);
        $mkdirUrl = 'uploads'.'/'.$time;
//        $mkdirUrl = env("UPLOAD_DIR");
        $path1 = $file1-> move($mkdirUrl,$newName1); //把缓存文件移动到制定文件夹
        $path2 = $file2-> move($mkdirUrl,$newName2); //把缓存文件移动到制定文件夹
        $path_url1 =  $uploadFolder.$newName1;
        $path_url2 =  $uploadFolder.$newName2;
//        $path_url1 =  env("UPLOAD_URL").env("UPLOAD_DIR").$newName1;
//        $path_url2 =  env("UPLOAD_URL").env("UPLOAD_DIR").$newName2;
        $data = array(
            'img1' => $path_url1,
            'img2' => $path_url2
        );
        if($path1 && $path2){
            return $this->return_json(200,$data,'上传成功');
        }else{
            return $this->return_json(201,'','上传失败');
        }
    }

}
